package org.cargloss.cp.controller.modul.event;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.cargloss.cp.model.Event;

import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionCrud extends ActionSupport {
	private static final long serialVersionUID = -5933849517817526086L;
	private String id;
	private String eventDate;
	private String location;
	private String title;
	private String content;
	private String createdBy;
	private String modifiedBy;
	
	Date date = new Date();
	Timestamp time = new Timestamp(date.getTime());

	private Timestamp getNow() 
	{
		Calendar calendar = Calendar.getInstance();
		Date now = calendar.getTime();
		return new Timestamp(now.getTime());
	}

	public String insert() {
		DAO<Integer> dao = new DAO<Integer>();
		Event object = new Event();

		if (!getEventDate().equals(""))
			object.setEventDate(Timestamp.valueOf(getEventDate() + ":00"));

		object.setTitle(getTitle());
		object.setLocation(getLocation());
		object.setContent(getContent());
		object.setCreatedBy(getCreatedBy());
		object.setModifiedBy(getModifiedBy());
		object.setCreatedDate(getNow());
		object.setModifiedDate(getNow());

		dao.insert(object);
		dao.close();
		return "empty";
	}

	public String update() {
		DAO<Integer> dao = new DAO<Integer>();
		Event object = (Event) dao.find(Event.class, Integer.valueOf(getId()));

		if (!getEventDate().equals(""))
			object.setEventDate(Timestamp.valueOf(getEventDate() + ":00"));

		object.setTitle(getTitle());
		object.setLocation(getLocation());
		object.setContent(getContent());
		object.setCreatedBy(getCreatedBy());
		object.setModifiedBy(getModifiedBy());
		object.setCreatedDate(getNow());
		object.setModifiedDate(getNow());

		dao.update(object);
		dao.close();
		return "empty";
	}

	public String delete() {
		DAO<Integer> dao = new DAO<Integer>();
		Event delete = (Event) dao.find(Event.class, Integer.valueOf(getId()));
		dao.delete(delete);
		dao.close();
		return "empty";
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getEventDate() {
		return eventDate;
	}

	public void setEventDate(String eventDate) {
		this.eventDate = eventDate;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	
	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	
	public String getModifiedBy() {
		return modifiedBy;
	}

	public void setModifiedBy(String modifiedBy) {
		this.modifiedBy = modifiedBy;
	}

}
